package com.zbc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zbc.annotation.AutoFill;
import com.zbc.constant.JwtClaimsConstant;
import com.zbc.constant.MessageConstant;
import com.zbc.constant.PasswordConstant;
import com.zbc.constant.StatusConstant;
import com.zbc.context.BaseContext;
import com.zbc.dto.EmployeeDTO;
import com.zbc.dto.EmployeeLoginDTO;
import com.zbc.dto.EmployeePageDTO;
import com.zbc.entity.Employee;
import com.zbc.enumeration.OperationType;
import com.zbc.exception.AccountLockedException;
import com.zbc.exception.AccountNotFoundException;
import com.zbc.exception.PasswordErrorException;
import com.zbc.mapper.EmployeeMapper;
import com.zbc.properties.JwtProperties;
import com.zbc.result.PageResult;
import com.zbc.service.EmployeeService;
import com.zbc.utils.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Map;


@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        Employee employee = employeeMapper.getByUsername(username);

        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(employee.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if(employee.getStatus() == StatusConstant.DISABLE){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return employee;

    }

    @Override
    public void insertEmployee(EmployeeDTO employeeDTO) {

        Employee employee = new Employee();

        BeanUtils.copyProperties(employeeDTO, employee,"id");

        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        employee.setStatus(StatusConstant.ENABLE);

        employeeMapper.insertEmployee(employee);
    }

    @Override
    public PageResult pageQuery(EmployeePageDTO employeePageDTO) {
        PageHelper.startPage(employeePageDTO.getPage(),employeePageDTO.getPageSize());
        Page<Employee> pageQuery = employeeMapper.pageQuery(employeePageDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal((int) pageQuery.getTotal());
        pageResult.setRecords(pageQuery.getResult());
        return pageResult;
    }

    @Override
    public void updateEmployeeStatus(Integer status, Integer id) {
        Employee employee = Employee.builder()
                .status(status)
                .id(id)
                .build();

        employeeMapper.update(employee);
    }

    @Override
    public Employee getEmployeeById(Integer id) {
        Employee employee = employeeMapper.getEmployeeById(id);
        employee.setPassword("");
        return employee;
    }

    @Override
    public void updateEmployee(Employee employee) {
        Employee emp = Employee.builder()
                .id(employee.getId())
                .idNumber(employee.getIdNumber())
                .name(employee.getName())
                .phone(employee.getPhone())
                .sex(employee.getSex())
                .username(employee.getUsername())
                .build();
        employeeMapper.update(emp);
    }
}
